package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.PermissionService;
import com.atguigu.ssyx.acl.service.RolePermissionService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.acl.Permission;
import com.atguigu.ssyx.model.acl.RolePermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单管理 前端控制器
 */

@RestController
@RequestMapping("/admin/acl/permission")
@Api(tags = "菜单管理")//Permission
public class PermissionController {

    @Autowired
    private RolePermissionService rolePermissionService;
    @Autowired
    private PermissionService permissionService;

    /*
  获取权限(菜单/功能)列表
  */
//    getPermissionList() {
//        return request({
//                url: `${api_name}`,
//        method: 'get'
//    })
//    },

    @ApiOperation(value = "获取菜单")
    @GetMapping
    public Result index() {
        List<Permission> list = permissionService.queryAllMenu();
        return Result.ok(list);
    }


//    addPermission(permission) {
//        return request({
//                url: `${api_name}/save`,
//        method: 'post',
//                data: permission
//    })
//    },


    @ApiOperation(value = "新增菜单")
    @PostMapping("save")
    public Result save(@RequestBody Permission permission) {
        permissionService.save(permission);
        return Result.ok(null);
    }


    @ApiOperation(value = "修改菜单")
    @PutMapping("update")
    public Result updateById(@RequestBody Permission permission) {
        permissionService.updateById(permission);
        return Result.ok(null);

    }


    @ApiOperation(value = "递归删除菜单")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        permissionService.removeChildById(id);
        return Result.ok(null);
    }


    @ApiOperation(value = "根据角色查询权限")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long Id) {
        Map map = new HashMap();

        LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();

//        rolePermissionService.

        return Result.ok(null);
    }

    @ApiOperation("查看某个角色的权限列表")
    @GetMapping({"/toAssign/{roleId}"})
    public Result toAssign(@PathVariable Long roleId) {
        LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RolePermission::getRoleId, roleId);
        List<RolePermission> list = rolePermissionService.list(wrapper);
        Map<String, Object> permissionList = rolePermissionService.getPermissionList(list);
//        拿出角色权限表的角色id,拿出多个权限id.使用for循环遍历还是    where id in  ()  呢?
//这里是使用for循环的.
        return Result.ok(permissionList.get("assignRoles"));
    }


}
